<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class PinglunController extends Controller
{
    public function getIndex(Request $request){
        // 商品id
        $id = $request -> input('id');
        $data = DB::table('goods') -> where('id',$id)-> first();
        // dd($data);
        if(empty(session('id'))){
            return redirect('home/login')->with('error','请您登陆后进行评价，感谢您的配合');
        }
 return view('home/pinglun/index',['gname'=>$data['name']]);
    }
    public function postInsert(Request $request){
        
        // 获取用户数据
        $y = DB::table('user')->where('id',session('id')) -> first();
        // 评论内容
        $arr = $request -> only('content');
      //评论用户
       $arr['uname'] = $y['username'];
       // 评论时间
       $arr['createtime'] = date('Y-m-d H:i:s',time());
       // 评论商品
       $arr['gname'] = $request -> input('gname');
        // dd($arr);
        $data = DB::table('pinglun') -> insert($arr);
        if($data){
            return redirect('/home');
        }else{
            return back();
        }
    }
}
